package com.bidevalution.business.function;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.Map;

import com.googlecode.aviator.runtime.type.AviatorString;
import org.springframework.util.StringUtils;

import com.googlecode.aviator.runtime.function.AbstractFunction;
import com.googlecode.aviator.runtime.function.FunctionUtils;
import com.googlecode.aviator.runtime.type.AviatorDouble;
import com.googlecode.aviator.runtime.type.AviatorObject;

public class DoubleScaleFunction  extends AbstractFunction{
	public DoubleScaleFunction(FunctionInitializer functionInitializer) {
		super();
	}
	/**
	 * 返回数组中跟某个值进行比较的值范围
	 * <p>Description: </p>
	 * @param env
	 * @param arg1 数组
	 * @param arg2 比较符号 lt小于 lte小于等于 gt大于 gte大于或等于 eq等于
	 * @param arg3 比较值
	 * @return 返回例如:1,2,3,4
	 * @see com.googlecode.aviator.runtime.function.AbstractFunction#call(java.util.Map, com.googlecode.aviator.runtime.type.AviatorObject, com.googlecode.aviator.runtime.type.AviatorObject, com.googlecode.aviator.runtime.type.AviatorObject)
	 */
	@Override
	public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2) {
		Double paramValue = FunctionUtils.getNumberValue(arg1, env).doubleValue();

		Long scale = FunctionUtils.getNumberValue(arg2, env).longValue();
		NumberFormat nf = NumberFormat.getInstance();
		//设置保留多少位小数
		nf.setMaximumFractionDigits(scale.intValue());
		// 取消科学计数法
		nf.setGroupingUsed(false);
		//返回结果
		return new AviatorString(String.valueOf(nf.format(paramValue)));
	}
	@Override
	public String getName() {
		return "doubleScale";
	}
}
